/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.jome.ctrl.amto;

/**
 *
 * @author Mr.Sam
 */
import fr.ove.openmath.jome.ctrl.linear.events.LinearParserEvent;
import fr.ove.openmath.jome.ctrl.amto.MapsToNAry;

/**
* The abstract math tree object of the power.
*
* @author © 1999 DIRAT Laurent
* @version 1.0  18/11/1999
*/
public class Power extends MapsToNAry {
    /**
    * The Constructor.
    */
    public Power() {
        super("power", LinearParserEvent.POWER);
    }

    /**
    * "Flushes" the specified child.<BR>
    * According to their father, some children need to be between parenthesis to respect correctness of the
    * expression. So, when needed, the specified child is "flushed" as if it was between parenthesis.
    * @param child the child to "flush"
    * @param index the child index in the list of children of the instance.
    */
    protected void flushChild(AbstractMathTreeObject child, int index){
        // (^ a (^ b c)) ==> a^(b^c) alors que (^ (^ a b) c) ==> (a^b)^c=a^b^c
        if ((child.identifier.equals("power") && (index == 1)) ||
            child.identifier.equals("minus") || child.identifier.equals("plus") || child.identifier.equals("unaryMinus") || 
            child.identifier.equals("divide") || child.identifier.equals("arithTimes")) {
            LinearParserEvent linearParserEvent = new LinearParserEvent(getEventSource());
            linearParserEvent.setToken(LinearParserEvent.OPEN_PAREN, "(");
            fireLinearParserEvent(linearParserEvent);
            child.flush();
            linearParserEvent.setToken(LinearParserEvent.CLOSE_PAREN, ")");
            fireLinearParserEvent(linearParserEvent);
        }
        else
            child.flush();
    }
}